package com.zx.flow.api.util;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.zx.flow.api.exception.FlowException;
import com.zx.flow.api.execute.model.FlowContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SendUtil {

    private static final int[] REPLAY_LEVEL_TIME = {1 * 1000, 5 * 1000, 10 * 1000, 30 * 1000, 60 * 1000};

    public static String httpPost(Object obj, String url, int connectionTimeout, int readTimeout, int replay) {
        for (int i = 0; i < replay; i++) {
            try (HttpResponse httpResponse = HttpUtil.createPost(url)
                    .setReadTimeout(readTimeout)
                    .setConnectionTimeout(connectionTimeout)
                    // 将流程对象发送过去
                    .body(Util.getJsonStr(obj))
                    .execute()
            ) {
                if (httpResponse.isOk()) {
                    String body = httpResponse.body();
                    return body;
                } else {
                    log.warn("http失败, status={}, response={}", httpResponse.getStatus(), httpResponse.body());
                }
            } catch (Exception e) {
                log.error("http异常", e);
            }
            ThreadUtil.safeSleep(REPLAY_LEVEL_TIME[i]);
        }
        throw new FlowException("请求失败,重试" + replay + "次");
    }

    public static String wechat(FlowContext flow, String msgTemplateId) {
        // 调用内置接口
        return "";
    }

    public static String dingding(FlowContext flow, String msgTemplateId) {
        // 调用内置接口
        return "";
    }

    public static String feishu(FlowContext flow, String msgTemplateId) {
        // 调用内置接口
        return "";
    }

    public static String sms(FlowContext flow, String msgTemplateId) {
        // 调用内置接口
        return "";
    }

    public static String mq(FlowContext flow, String toptic, String tag) {
        // 调用内置接口
        return "";
    }

}
